package com.galacticconversion.command;

import com.galacticconversion.model.DataStorage;
import com.galacticconversion.util.RomanNumberUtil;

/**
 * 处理金属价格命令 (如 "glob glob Silver is 34 Credits")
 */
public class MetalPriceCommand implements Command {
    private final DataStorage dataStorage;

    public MetalPriceCommand(DataStorage dataStorage) {
        this.dataStorage = dataStorage;
    }

    @Override
    public void execute(String input) {
        String[] parts = input.split(" is ");
        int credits = Integer.parseInt(parts[1].split(" ")[0]);
        String[] metalParts = parts[0].split(" ");
        String metal = metalParts[metalParts.length - 1];

        // 提取星际单位部分
        StringBuilder galacticUnits = new StringBuilder();
        for (int i = 0; i < metalParts.length - 1; i++) {
            galacticUnits.append(metalParts[i]).append(" ");
        }

        // 转换为罗马数字并计算价值
        String roman = RomanNumberUtil.convertGalacticToRoman(galacticUnits.toString().trim(),dataStorage);
        int value = RomanNumberUtil.romanToDecimal(roman);
        if (value > 0) {
            dataStorage.getMetalPrices().put(metal, (double) credits / value);
        }
    }


}
